home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gdevn533.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  210 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevn533.c */
  20. /* Sony NWP-533 driver for GhostScript */
  21. #include "gdevprn.h"
  22. #include <sys/ioctl.h>
  23. #include <newsiop/lbp.h>
  24.  
  25. /***
  26.  *** Note: this driver was contributed by a user, Tero Kivinen:
  27.  ***       please contact kivinen@joker.cs.hut.fi if you have questions.
  28.  ***/
  29.  
  30. #define A4_PAPER 1
  31.  
  32. #ifdef A4_PAPER
  33. #define PAPER_XDOTS A4_XDOTS
  34. #define PAPER_YDOTS A4_YDOTS
  35. #else
  36. #define PAPER_XDOTS B4_XDOTS
  37. #define PAPER_YDOTS B4_YDOTS
  38. #endif
  39.  
  40. #ifndef TRUE
  41. #define TRUE 1
  42. #endif
  43. #ifndef FALSE
  44. #define FALSE 0
  45. #endif
  46.  
  47. /* The device descriptor */
  48. private dev_proc_open_device(nwp533_open);
  49. private dev_proc_print_page(nwp533_print_page);
  50. private dev_proc_close_device(nwp533_close);
  51. private gx_device_procs nwp533_procs =
  52.   prn_procs(nwp533_open, gdev_prn_output_page, nwp533_close);
  53.  
  54. gx_device_printer gs_nwp533_device =
  55.   prn_device(nwp533_procs, "nwp533",
  56.     PAPER_XDOTS * 10.0 / DPI,    /* width_10ths */
  57.     PAPER_YDOTS * 10.0 / DPI,    /* height_10ths */
  58.     DPI,                /* x_dpi */
  59.     DPI,                /* y_dpi */
  60.     0,0,0,0,            /* margins */
  61.     1, nwp533_print_page);
  62.  
  63. /* return True if should retry - False if should quit */
  64. private int
  65. analyze_error(int printer_file)
  66. {
  67.   struct lbp_stat status;
  68.   char *detail = NULL, *old_detail = NULL;
  69.   int waiting = TRUE;
  70.   int retry_after_return = TRUE;
  71.  
  72.   if(ioctl(printer_file, LBIOCRESET, 0) < 0)
  73.     {
  74.       perror("ioctl(LBIOCRESET)");
  75.       return FALSE;
  76.     }
  77.   if (ioctl(printer_file, LBIOCSTATUS, &status) < 0)
  78.     {
  79.       perror("ioctl(LBIOCSTATUS)");
  80.       return FALSE;
  81.     }
  82.  
  83.   do
  84.     {
  85.       /* Is there an error */
  86.       if(status.stat[0] & (ST0_CALL | ST0_REPRINT_REQ | ST0_WAIT | ST0_PAUSE))
  87.     {
  88.       if(status.stat[1] & ST1_NO_CARTRIGE)/* mispelled? */
  89.         detail = "No cartridge - waiting";
  90.       else if(status.stat[1] & ST1_NO_PAPER)
  91.         detail = "Out of paper - waiting";
  92.       else if(status.stat[1] & ST1_JAM)
  93.         detail = "Paper jam - waiting";
  94.       else if(status.stat[1] & ST1_OPEN)
  95.         detail = "Door open - waiting";
  96.       else if(status.stat[1] & ST1_TEST)
  97.         detail = "Test printing - waiting";
  98.       else {
  99.         waiting = FALSE;
  100.         retry_after_return = FALSE;
  101.         
  102.         if(status.stat[2] & ST2_FIXER)
  103.           detail = "Fixer trouble - quiting";
  104.         else if(status.stat[2] & ST2_SCANNER)
  105.           detail = "Scanner trouble - quiting";
  106.         else if(status.stat[2] & ST2_MOTOR)
  107.           detail = "Scanner motor trouble - quiting";
  108.         else if(status.stat[5] & ST5_NO_TONER)
  109.           detail = "No toner - quiting";
  110.       }
  111.     }
  112.       else
  113.     {
  114.       waiting = FALSE;
  115.     }
  116.       if(detail != NULL && detail != old_detail)
  117.     {
  118.       perror(detail);
  119.       old_detail = detail;
  120.     }
  121.       if(waiting)
  122.     {
  123.       ioctl(1, LBIOCRESET, 0);
  124.       sleep(5);
  125.       ioctl(1, LBIOCSTATUS, &status);
  126.     }
  127.     }
  128.   while(waiting);
  129.   return retry_after_return;
  130. }
  131.  
  132. private int
  133. nwp533_open(gx_device *dev)
  134. {
  135.   gx_device_printer *pdev = (gx_device_printer *) dev;
  136.  
  137.   if (pdev->fname[0] == '\0')
  138.     {
  139.       strcpy(pdev->fname, "/dev/lbp");
  140.     }
  141.   return gdev_prn_open(dev);
  142. }
  143.  
  144. private int
  145. nwp533_close(gx_device *dev)
  146. {
  147.   if (((gx_device_printer *) dev)->file != NULL)
  148.     {
  149.       int printer_file;
  150.       
  151.       printer_file = fileno(((gx_device_printer *) dev)->file);
  152.     restart2:
  153.       if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
  154.     {
  155.       if(analyze_error(printer_file))
  156.         goto restart2;
  157.       perror("Waiting for device");
  158.       return_error(gs_error_ioerror);
  159.     }
  160.     }
  161.   return gdev_prn_close(dev);
  162. }
  163.  
  164. /* Send the page to the printer. */
  165. private int
  166. nwp533_print_page(gx_device_printer *dev, FILE *prn_stream)
  167. {
  168.   int lnum;
  169.   int line_size = gdev_mem_bytes_per_scan_line(dev);
  170.   byte *in;
  171.   int printer_file;
  172.   printer_file = fileno(prn_stream);
  173.   
  174.   if (line_size % 4 != 0)
  175.     {
  176.       line_size += 4 - (line_size % 4);
  177.     }
  178.   in = (byte *) gs_malloc(line_size, 1, "nwp533_output_page(in)");
  179.  restart:
  180.   if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
  181.     {
  182.       if(analyze_error(printer_file))
  183.     goto restart;
  184.       perror("Waiting for device");
  185.       return_error(gs_error_ioerror);
  186.     }
  187.   lseek(printer_file, 0, 0);
  188.   
  189.   for ( lnum = 0; lnum < dev->height; lnum++)
  190.     {
  191.       gdev_prn_copy_scan_lines(prn_dev, lnum, in, line_size);
  192.       if(write(printer_file, in, line_size) != line_size)
  193.     {
  194.       perror("Writting to output");
  195.       return_error(gs_error_ioerror);
  196.     }
  197.     }
  198.  retry:
  199.   if(ioctl(printer_file, LBIOCSTART, 0) < 0)
  200.     {
  201.       if(analyze_error(printer_file))
  202.     goto retry;
  203.       perror("Starting print");
  204.       return_error(gs_error_ioerror);
  205.     }
  206.   gs_free(in, line_size, 1, "nwp533_output_page(in)");
  207.  
  208.   return 0;
  209. }
  210.